Skip to content

Add unit tests for Cloudflare Worker (Phase 0) - #212

Merged
richardthe3rd merged 7 commits into
mainfrom
claude/cloudflare-workers-testing-ZSpKL
Feb 8, 2026
Merged

Add unit tests for Cloudflare Worker (Phase 0)#212
richardthe3rd merged 7 commits into
mainfrom
claude/cloudflare-workers-testing-ZSpKL

Conversation

@richardthe3rd

Copy link
Copy Markdown
Owner

Set up Vitest + @cloudflare/vitest-pool-workers test infrastructure for the
existing data proxy worker. This establishes the Worker testing pattern before
building the ratings Worker and catches regressions in security-relevant CORS
logic.

Test coverage (55 tests across 4 files):

  • cors.test.js: Origin matching for all allowed origins (production, staging,
    Pages previews, localhost, tunnel) and rejection of unknown origins. Preflight
    handling with correct max-age (300s production, 10s staging/dev).
  • festivals.test.js: Embedded festivals.json serving, cache headers, content
    type, CORS headers, and /festivals alias.
  • beverage-types.test.js: HTML directory listing parsing, alphabetical sorting,
    filtering of self-referential files, 404/500 error handling, cache headers.
  • proxy.test.js: Health check endpoint, upstream proxying with charset
    enforcement, error handling (502), CORS headers on all responses.

CI integration:

  • Add test-worker job to deploy-worker.yml that runs on worker or festivals
    changes
  • Both validate-worker and deploy-worker now depend on test-worker passing

https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L

Set up Vitest + @cloudflare/vitest-pool-workers test infrastructure for the
existing data proxy worker. This establishes the Worker testing pattern before
building the ratings Worker and catches regressions in security-relevant CORS
logic.

Test coverage (55 tests across 4 files):
- cors.test.js: Origin matching for all allowed origins (production, staging,
  Pages previews, localhost, tunnel) and rejection of unknown origins. Preflight
  handling with correct max-age (300s production, 10s staging/dev).
- festivals.test.js: Embedded festivals.json serving, cache headers, content
  type, CORS headers, and /festivals alias.
- beverage-types.test.js: HTML directory listing parsing, alphabetical sorting,
  filtering of self-referential files, 404/500 error handling, cache headers.
- proxy.test.js: Health check endpoint, upstream proxying with charset
  enforcement, error handling (502), CORS headers on all responses.

CI integration:
- Add test-worker job to deploy-worker.yml that runs on worker or festivals
  changes
- Both validate-worker and deploy-worker now depend on test-worker passing

https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
- Add test for CORS headers on beverage-types 500 error response
  (ensures browsers can read error details cross-origin)
- Add Vary header assertion on rejected origins in CORS tests
  (verifies no cache-related headers leak for unknown origins)

https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
- test:worker: Install deps and run worker tests (npm install && npm test)
- Uses sources for caching: skips when worker code/tests/deps unchanged

https://claude.ai/code/session_01Gc9AXJisQdxqBwg73A447L
Copilot AI review requested due to automatic review settings February 8, 2026 20:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Vitest-based unit test harness for the existing Cloudflare data-proxy Worker, and wires it into CI so CORS/proxy behavior regressions are caught early.

Changes:

  • Add Vitest + @cloudflare/vitest-pool-workers configuration for running Worker tests under workerd.
  • Introduce a test suite covering CORS, festivals registry serving, beverage type discovery parsing, and upstream proxy behavior.
  • Add a test-worker CI job and make worker validation/deploy depend on it; add a mise task to run Worker tests locally.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
mise.toml Adds a test:worker task for running Worker tests locally.
cloudflare-worker/vitest.config.js Configures Vitest to run Worker tests via @cloudflare/vitest-pool-workers.
cloudflare-worker/test/cors.test.js Tests allowed/rejected origins and preflight max-age behavior.
cloudflare-worker/test/festivals.test.js Tests embedded festivals.json serving, headers, and /festivals alias.
cloudflare-worker/test/beverage-types.test.js Tests directory listing parsing into beverage types, sorting, filtering, and error paths.
cloudflare-worker/test/proxy.test.js Tests /health, upstream proxying behavior, charset enforcement, and error handling.
cloudflare-worker/package.json Adds Worker test scripts and dev dependencies for Vitest + workers pool.
cloudflare-worker/package-lock.json Locks new test dependencies.
.github/workflows/deploy-worker.yml Adds test-worker job and makes validate/deploy depend on tests passing.

Comment thread mise.toml Outdated
description = "Run Cloudflare Worker tests (Vitest + workerd)"
dir = "cloudflare-worker"
sources = ['cloudflare-worker/package.json', 'cloudflare-worker/package-lock.json', 'cloudflare-worker/worker.js', 'cloudflare-worker/test/**/*.js', 'cloudflare-worker/vitest.config.js', 'data/festivals.json']
run = 'npm install && npm test'

Copilot AI Feb 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mise test:worker task runs npm install, which is non-deterministic and can update package-lock.json during the task run. Since the worker has a lockfile and CI uses npm ci, prefer npm ci here as well to keep local runs reproducible and avoid dirty working trees.

Suggested change
run = 'npm install && npm test'
run = 'npm ci && npm test'

Copilot uses AI. Check for mistakes.
Comment thread .github/workflows/deploy-worker.yml Outdated
Comment on lines +94 to +96
- name: Copy festivals.json to worker directory
run: cp data/festivals.json cloudflare-worker/festivals.json

Copilot AI Feb 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In test-worker, the explicit cp data/festivals.json cloudflare-worker/festivals.json is redundant because npm test will run the worker’s pretest script, which already copies ../data/festivals.json into ./festivals.json. Consider removing one of these copies (e.g., drop the workflow step for test-worker) to avoid duplication and keep the source of truth in one place.

Suggested change
- name: Copy festivals.json to worker directory
run: cp data/festivals.json cloudflare-worker/festivals.json

Copilot uses AI. Check for mistakes.
@richardthe3rd

Copy link
Copy Markdown
Owner Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copilot AI commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

@richardthe3rd I've opened a new pull request, #213, to work on those changes. Once the pull request is ready, I'll request review from you.

@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

LCOV of commit e1952a8 during CI #39

Summary coverage rate:
  lines......: 77.5% (2235 of 2883 lines)
  functions..: no data found
  branches...: no data found

Files changed coverage rate: n/a

@codecov

codecov Bot commented Feb 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

… copy

Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

🚀 Cloudflare Pages Preview

Your preview deployment is ready!

Preview URL: https://claude-cloudflare-workers-te.staging-cambeerfestival.pages.dev

This preview will be automatically updated when you push new commits to this PR.

Apply review feedback: use npm ci and remove redundant festivals.json copy
@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

🚀 Cloudflare Pages Preview

Your preview deployment is ready!

Preview URL: https://claude-cloudflare-workers-te.staging-cambeerfestival.pages.dev

This preview will be automatically updated when you push new commits to this PR.

@richardthe3rd
richardthe3rd merged commit 5d7cb72 into main Feb 8, 2026
16 checks passed
@richardthe3rd
richardthe3rd deleted the claude/cloudflare-workers-testing-ZSpKL branch February 8, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants